package cn.template.serve_project.controller;

import cn.template.serve_project.aop.note.Authentication;
import cn.template.serve_project.dto.excel.ExcelUserDTO;
import cn.template.serve_project.dto.user.UserAddDTO;
import cn.template.serve_project.dto.user.UserPageDTO;
import cn.template.serve_project.dto.user.UserUpdateDTO;
import cn.template.serve_project.encrypt.rsa.RSAUtil;
import cn.template.serve_project.enums.RoleEnum;
import cn.template.serve_project.result.Result;
import cn.template.serve_project.service.UserService;
import cn.template.serve_project.vo.user.UserPageVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 用户表(User)表控制层
 *
 */
@RestController
@RequestMapping("user")
@Validated
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;


    /**
     * 新增用户(Admin)
     *
     * @param dto 参数对象
     * @return 用户id
     */
    @Authentication(role = RoleEnum.ADMIN)
    @PostMapping("/auth/add")
    public Result<?> addUser(@Valid @RequestBody UserAddDTO dto) {
        userService.addUser(dto);
        return Result.success();
    }


    /**
     * 分页查询用户信息(Admin)
     *
     * @param dto 参数对象
     * @return 数据列表
     */
    @Authentication(role = RoleEnum.ADMIN)
    @GetMapping("/auth/page")
    public Result<UserPageVO> page(@Valid @ModelAttribute UserPageDTO dto) {
        return Result.success(userService.selectPage(dto));
    }


    /**
     * 导出excel文件
     *
     * @param excelObj 参数对象
     * @param response 请求对象
     */
    @Authentication(role = RoleEnum.SUPER_ADMIN)
    @PostMapping("/auth/export/excel")
    public void exportExcel(@Valid @RequestBody ExcelUserDTO excelObj, HttpServletResponse response) throws IOException {
        userService.export(excelObj, response);
    }


    /**
     * 根据id列表删除(Admin)
     *
     * @param idList id列表
     * @return 成功数
     */
    @Authentication(role = RoleEnum.SUPER_ADMIN)
    @PostMapping("/auth/del/id")
    public Result<Integer> deleteInIdList(@Valid @RequestBody List<Long> idList) {
        return Result.success(userService.deleteInIdList(idList));
    }


    /**
     * 变更用户信息(Admin)
     *
     * @param user 参数对象
     * @return 成功数
     */
    @Authentication(role = RoleEnum.ADMIN)
    @PostMapping("/auth/update")
    public Result<?> updateUser(@Valid @RequestBody UserUpdateDTO user) {
        userService.updateUser(user);
        return Result.success();
    }



}

